// pages/mine/bbs/bbs.js
import {
    HTTP
} from '../../../utils/http.js'
let ajax = new HTTP()
const app = getApp()
Page({
    data: {
        defaultImg: "",
        defaultName: "",
        memberid: "",
        isself: 0,
        dtnum: 0,
        gznum: 0,
        fansnum: 0,
        allArr: [],
        loading: false
    },
    onLoad(options) {
        wx.showLoading({
            title: '加载中',
        })
        let userInfo = app.globalData.userInfo
        let memberid = wx.getStorageSync("memberid")
        this.setData({
            defaultName: userInfo.nickName,
            defaultImg: userInfo.avatarUrl,
            memberid: memberid,
            isself: 1
        })
        this._getmyinfo(memberid, 1)
        this._getBbsarticle(memberid)
    },
    onShow () {
        this._getmyinfo(app.globalData.memberid, 1)
    },
    onConcernTap(event) {
        if (this.data.gznum == 0) {
            wx.showToast({
                title: '好伤心，还没有人关注你呢',
                icon: 'none'
            })
            return
        }
        wx.navigateTo({
            url: 'concern/concern',
        })
    },
    onFansTap(event) {
        if (this.data.fansnum == 0) {
            wx.showToast({
                title: '好伤心，你还没有粉丝呢',
                icon: 'none'
            })
            return
        }
        wx.navigateTo({
            url: 'fans/fans',
        })
    },
    onFavourTap(event) {
        wx.navigateTo({
            url: 'favour/favour',
        })
    },
    // 跳转动态详情
    gotoBbsdetail(event) {
        let articleId = event.currentTarget.dataset.articleid
        let title = event.currentTarget.dataset.title
        wx.navigateTo({
            url: '/pages/community/detail/detail?id=' + articleId + '&title=' + title,
        })
    },
    // 删除当前动态
    deleteBBS (event) {
        let id = event.currentTarget.dataset.id
        let that = this
        console.log(this.data.dtnum)
        wx.showModal({
            content: '确认删除该条动态？',
            success (res) {
                if (res.confirm) {
                    ajax.request({
                        method: "GET",
                        url: "mycenter",
                        data: {
                            actname: "delarticle",
                            memberid: that.data.memberid,
                            id: id
                        },
                        success: (res) => {
                            if (res.rescode === "0000") {
                                wx.showToast({
                                    title: "删除动态成功",
                                    icon: "success",
                                    duration: 1500
                                })
                                // 删除数组中的数据
                                for (let i = 0; i < that.data.allArr.length; i++) {
                                    if (that.data.allArr[i].id === id) {
                                        that.data.allArr.splice(i, 1)
                                        that.data.dtnum--
                                        that.setData({
                                            allArr: that.data.allArr,
                                            dtnum: that.data.dtnum
                                        })
                                    }
                                }
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log(222)
                }
            }
        })
    },
    // 获取个人信息
    _getmyinfo(nmemberid, isself) {
        ajax.request({
            method: "GET",
            url: "mycenter",
            data: {
                actname: 'myinfo',
                memberid: nmemberid,
            },
            success: (res) => {
                if (res.rescode == "0000") {
                    if (isself == 1) {
                        console.log(res.data)
                        this.setData({
                            dtnum: res.data.dtnum,
                            fansnum: res.data.fansnum,
                            gznum: res.data.gznum
                        })
                    } else {
                        this.setData({
                            defaultName: res.data.nickname,
                            defaultImg: res.data.headimgurl,
                            memberid: nmemberid,
                            isself: 0,
                            dtnum: res.data.dtnum,
                            fansnum: res.data.fansnum,
                            gznum: res.data.gznum
                        })
                    }
                }
            }
        })
    },
    // 获取个人动态
    _getBbsarticle(nmemberid) {
        ajax.request({
            method: "GET",
            url: "mycenter",
            data: {
                actname: 'myarticle',
                memberid: nmemberid,
                p: 0
            },
            success: (res) => {
                wx.hideLoading()
                if (res.rescode == "0000") {
                    this.setData({
                        allArr: res.data,
                        loading: true
                    })
                } else {
                    this.setData({
                        loading: true
                    })
                }
            }
        })
    }
})